package com.hzjq.core.impl

import com.hzjq.core.ErrorCode
import com.hzjq.core.bean.AlongCapResultEntity
import com.hzjq.core.callback.Callback
import com.hzjq.core.loader.AlongCapCheckLoader
import com.hzjq.core.worker.AlongCapCheckWork
import io.reactivex.functions.Consumer

class AlongCapCheckImpl : AlongCapCheckLoader {

    private var alongCapCheckWork:AlongCapCheckWork?=null

    override fun start(callback: Callback<AlongCapResultEntity>) {
        CheckerImpl().setCallback(Consumer {
            if (it) {
                alongCapCheckWork?.cancel()
                alongCapCheckWork = AlongCapCheckWork(callback)
                alongCapCheckWork?.doWork()
            } else {
                callback.onError(ErrorCode.getErrorResult(103))
            }
        }).start()
    }

    override fun cancel() {
        alongCapCheckWork?.cancel()
    }
}